package com.statistics.dbutils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class DBUtils {
	private static final String OPTION_FILE_NAME = "db";
	private static String DRIVER ;
	private static String URL ;
	private static String USER ;
	private static String PASS ;
	
	static {
		ResourceBundle res = ResourceBundle.getBundle(OPTION_FILE_NAME) ;
		DRIVER = res.getString("DRIVER").trim() ;
		URL = res.getString("URL").trim() ;
		USER = res.getString("USER").trim() ;
		PASS = res.getString("PASSWORD").trim() ;
	}
	
	public static Connection getConnection() throws SQLException{
		Connection conn = null ;
		try {
			Class.forName(DRIVER) ;
			conn = DriverManager.getConnection(URL, USER, PASS) ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
		
		return conn ;
	}
	
	public static void close(Connection conn) {
		try {
			if(conn != null) {
				conn.close() ;
			}
		} catch (SQLException e) {
			e.printStackTrace() ;
		}
	}
	
	public static void close(Statement stmt) {
		try {
			if(stmt != null) {
				stmt.close() ;
			}
		} catch (SQLException e) {
			e.printStackTrace() ;
		}
	}
	
	public static void close(ResultSet rs) {
		try {
			if(rs != null) {
				rs.close() ;
			}
		} catch (SQLException e) {
			e.printStackTrace() ;
		}
	}
}
